import React, { Component } from 'react'
import {
  View,
  StyleSheet,
  ScrollView,
  Image,
  Text,
  TouchableOpacity,
  Switch
} from 'react-native'

import NavBar from '../../../../components/PublicHeader';
import { Constant } from '../../../../public/js/constant';
import fetchWebApi from '../../../../public/WebApi/fetchBase';
import {
  GET_N0ALLOWEDSEARCHME,
  POST_ALLOWEDSEARCHME,
  GET_NOTALLOWEDSOCIAL,
  POST_NOTALLOWEDSOCIAL
} from '../../../../public/WebApi/myInfoDataInterface/userInfoListApi';
import getStorageValue from '../../../../utils/getStorageValue';

export default class privacy extends Component {
  constructor(props) {
    super(props)
    this.state = {
      Topping: false,
      Topping2: false,
      params: '',
      showBusiness: null,
      business: null
    }
  }


  componentDidMount = () => {
    // 不允许搜索到我 初始化
    this.noAllowedSearchMe()
    // 社圈权限初始化
    this.getNotAllowedSocial()
  }
  // 处理不允许搜到我 点击
  noSearchMe() {
    if (this.state.Topping) {
      this.setState({ Topping: false })
      this.postAwedSearchMe("0")
    } else {
      this.setState({ Topping: true })
      this.postAwedSearchMe("1")
    }
  }
  // 处理不允许看我的社圈 点击
  notAllowedSocial = () => {
    if (this.state.Topping2) {
      this.setState({ Topping2: false })
      this.postNotAllowedSocial("0")
      this.setState({
        showBusiness: 0
      })
    } else {
      this.setState({ Topping2: true })
      this.postNotAllowedSocial("1")
      this.setState({
        showBusiness: 1
      })
    }
  }

  // 获取当前用户 不允许搜到我状态
  async noAllowedSearchMe() {
    const res = await fetchWebApi(GET_N0ALLOWEDSEARCHME,
      { token: await getStorageValue(Constant.TOKEN) }, 'GET');
    if (res.status == 200) {
      if (res.data[0].disturb == 1) {
        this.setState({
          Topping: true
        })
      } else {
        this.setState({
          Topping: false
        })
      }
    } else {
      Layer.showMsg("获取失败");
      return
    }
  }
  // 修改不允许搜到我
  async postAwedSearchMe(disturb) {
    const res = await fetchWebApi(POST_ALLOWEDSEARCHME,
      {
        token: await getStorageValue(Constant.TOKEN),
        disturb: disturb
      }, 'POST');
    if (res.status == 200) {
    } else {
      Layer.showMsg("修改失败");
      return
    }
  }

  // 社圈权限 获取
  async getNotAllowedSocial() {
    const res = await fetchWebApi(GET_NOTALLOWEDSOCIAL,
      { token: await getStorageValue(Constant.TOKEN) }, 'GET');
    if (res.status == 200) {
      this.setState({
        business: res.data[0].showBusiness
      })
      if (res.data[0].showBusiness == 1) {
        this.setState({
          Topping2: true,
          showBusiness: 1,
        })
      } else {
        this.setState({
          Topping2: false,
          showBusiness: 0
        })
      }
    } else {
      Layer.showMsg("获取失败");
      return
    }
  }
  // 修改不允许看到我
  async postNotAllowedSocial(showBusiness) {
    const res = await fetchWebApi(POST_NOTALLOWEDSOCIAL,
      {
        token: await getStorageValue(Constant.TOKEN),
        showBusiness: showBusiness
      }, 'POST');
    if (res.status == 200) {
    } else {
      Layer.showMsg("修改失败");
      return
    }
  }
  // 专递参数后 刷新
  getNotAlloweds() {
    this.getNotAllowedSocial()
  }

  render() {

    return (
      <View style={styles.warper} >
        <NavBar
          navigation={this.props.navigation}
          title={"隐私设置"}
        />
        <ScrollView>
          <View style={styles.privacy}>
            <View style={styles.privacy_list}>
              <Text style={styles.text}>不允许搜索到我</Text>
              <Switch
                trackColor={{ false: "#767577", true: "#34C759" }}
                ios_backgroundColor="#3e3e3e"
                value={this.state.Topping}
                onValueChange={() => this.noSearchMe()} />
            </View>
            <View style={styles.privacy_list}>
              <Text style={styles.text}>不允许看我的社圈</Text>
              <Switch
                trackColor={{ false: "#767577", true: "#34C759" }}
                ios_backgroundColor="#3e3e3e"
                value={this.state.Topping2}
                onValueChange={() => this.notAllowedSocial()} />
            </View>
          </View>
          {
            this.state.showBusiness == 0 ?
              <View style={styles.privacy}>
                <TouchableOpacity onPress={() => {
                  this.props.navigation.navigate('visibletime', {
                    params: {},
                    business: this.state.business,
                    // callBack获取子页面数据
                    callBack: (params) => {
                      this.setState({
                        params: params
                      })
                    },
                    postNotAllowedSocials: (showBusiness) => {
                      this.postNotAllowedSocial(showBusiness)
                    },
                    getNotAllowede: () => {
                      this.getNotAlloweds()
                    }
                  })
                }} style={styles.privacy_list}>
                  <Text style={styles.text}>社圈可见时间</Text>
                  <View style={styles.states}>
                    <Text style={styles.states_text}>{
                      this.state.business == 0 ? "全部" : null ||
                        this.state.business == 2 ? "最近半年" : null ||
                          this.state.business == 3 ? "最近一个月" : null}</Text>
                    <Image source={require('../../../../assets/images/maillist/Vector.png')} />
                  </View>
                </TouchableOpacity>
              </View>
              : null}
        </ScrollView>
      </View >
    )
  }
}
const styles = StyleSheet.create({
  container: {
    display: 'flex',
    flexDirection: 'row',
    alignItems: 'center',
    height: 32,
    backgroundColor: 'transparent'
  },
  privacy: {
    paddingLeft: 15,
    paddingRight: 15,
    backgroundColor: "#fff",
    marginTop: 10
  },
  privacy_list: {
    height: 55,
    alignItems: "center",
    flexDirection: 'row',
    justifyContent: 'space-between',
    backgroundColor: '#fff',
    borderBottomWidth: 1,
    borderBottomColor: '#f2f2f2',
  },
  states: {
    display: 'flex',
    flexDirection: 'row',
  },
  text: {
    fontSize: 16
  },
  states_text: {
    fontSize: 13,
    color: "#555",
    marginRight: 10
  }
})